package base;


import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.util.Collections;  

import javax.sound.midi.Sequencer;

public  class MouseHandler implements MouseMotionListener,MouseListener  
	{  
		Sequencer seq;
		Vector<Integer> posXVerde;
		Vector<Integer> posXVermelho;
		Vector<Integer> posXLilas;
		Vector<Integer> posXAzul;
		Vector<Integer> posXAmarelo;
		Vector<TempObject> posXVerdeO;
		Vector<TempObject> posXVermelhoO;
		Vector<TempObject> posXLilasO;
		Vector<TempObject> posXAzulO;
		Vector<TempObject> posXAmareloO;
		PanelColor pVerde;
		PanelColor pVermelho;
		PanelColor pLilas;
		PanelColor pAmarelo;
		PanelColor pAzul;

		MouseHandler(Sequencer seq,PanelColor[] panels){
			this.seq = seq;
			this.posXVerde = new Vector<Integer>();
			this.posXVermelho = new Vector<Integer>();
			this.posXLilas = new Vector<Integer>();
			this.posXAzul = new Vector<Integer>();
			this.posXAmarelo = new Vector<Integer>();
			this.posXVerdeO = new Vector<TempObject>();
			this.posXVermelhoO = new Vector<TempObject>();
			this.posXLilasO = new Vector<TempObject>();
			this.posXAzulO = new Vector<TempObject>();
			this.posXAmareloO = new Vector<TempObject>();
			this.pVerde = panels[0];
			this.pVermelho = panels[1];
			this.pLilas = panels[2];
			this.pAmarelo = panels[3];
			this.pAzul = panels[4];
			
		}

		public void mouseDragged(MouseEvent event) {}  

		public void mouseMoved(MouseEvent event) {} 



		public void mouseClicked(MouseEvent arg0) {

			Point p = new Point(); 
			p = MouseInfo.getPointerInfo().getLocation();  


			

          if(arg0.getButton() == 1){
            if(this.getPanelClicked(p.y).equals("verde")){
            	this.posXVerde.add(p.x);
            	this.posXVerdeO.add(new TempObject(p.x,3));
            	//pVerde.setPosX(this.posXVerde);
    			pVerde.setPosX2(this.posXVerdeO);
    			pVerde.repaint();
            }
            else if(this.getPanelClicked(p.y).equals("vermelho")){
            	this.posXVermelho.add(p.x);
            	this.posXVermelhoO.add(new TempObject(p.x,9));
    		//	pVermelho.setPosX(this.posXVermelho);
            	pVermelho.setPosX2(this.posXVermelhoO);
    			pVermelho.repaint();
            }
            else if(this.getPanelClicked(p.y).equals("lilas")){
            	this.posXLilas.add(p.x);
            	this.posXLilasO.add(new TempObject(p.x,5));
    		//	pLilas.setPosX(this.posXLilas);
            	pLilas.setPosX2(this.posXLilasO);
    			pLilas.repaint();
            }
            else if(this.getPanelClicked(p.y).equals("amarelo")){
            	this.posXAmarelo.add(p.x);
            	this.posXAmareloO.add(new TempObject(p.x,7));
    		//	pAmarelo.setPosX(this.posXAmarelo);
            	pAmarelo.setPosX2(this.posXAmareloO);
    			pAmarelo.repaint();
            }
            else{
            	this.posXAzul.add(p.x);
                this.posXAzulO.add(new TempObject(p.x,1));
    		//	pAzul.setPosX(this.posXAzul);
                pAzul.setPosX2(this.posXAzulO);
    			pAzul.repaint();
            }
			

			float timeMiliseq = seq.getMicrosecondLength()/1000;
//			System.out.println("Tempo em milissegundos: " + timeMiliseq);
			float timeArrow = ((p.x)*timeMiliseq)/pVerde.getWidth();
			int seg = (int)(timeArrow/1000);
			int min = seg/60;
			seg = seg -(min*60);
		//	System.out.println(min + ":" + seg);
		//	System.out.println("tempoClick:" + timeArrow);
          }
          else if(arg0.getButton() == 3){
        	  
        	  if(this.getPanelClicked(p.y).equals("verde")){
        		this.posXVerdeO.removeElement(new TempObject(p.x,3));
        	//	this.posXVerde.removeElement(p.x);
      			//pVerde.setPosX(this.posXVerde);
      			pVerde.setPosX2(this.posXVerdeO);
      			pVerde.repaint();
              }
              else if(this.getPanelClicked(p.y).equals("vermelho")){
            	  this.posXVermelhoO.removeElement(new TempObject(p.x,9));
             // 	this.posXVermelho.removeElement(p.x);
      			pVermelho.setPosX(this.posXVermelho);
      			pVermelho.repaint();
              }
              else if(this.getPanelClicked(p.y).equals("lilas")){
            	  this.posXLilasO.removeElement(new TempObject(p.x,5));
            //  	this.posXLilas.removeElement(p.x);
      			pLilas.setPosX(this.posXLilas);
      			pLilas.repaint();
              }
              else if(this.getPanelClicked(p.y).equals("amarelo")){
            	  this.posXAmareloO.removeElement(new TempObject(p.x,7));
           //   	this.posXAmarelo.removeElement(p.x);
      			pAmarelo.setPosX(this.posXAmarelo);
      			pAmarelo.repaint();
              }
              else{
            	  this.posXAzulO.removeElement(new TempObject(p.x,1));
           //   	this.posXAzul.removeElement(p.x);
      			pAzul.setPosX(this.posXAzul);
      			pAzul.repaint();
              }
          }
          else{
        	  this.saveMidi();
          }

		}

		String getPanelClicked(int y){
			String panel = "";
			if(y < 107){
				panel = "verde";
			}
			else if(y >= 107 && y <= 157){
				panel = "vermelho";
			}
			else if(y >= 157 && y <= 207){
				panel = "lilas";
			}
			else if(y >= 207 && y <= 257){
				panel = "amarelo";
			}
			else {
				panel = "azul";
			}


			return panel;
		}
		
		public Vector<SaveObject> saveMidi(){
			//this.posXVerde.s
			Vector<SaveObject> saveVerde = new Vector<SaveObject>();
			Vector<SaveObject> saveVermelho = new Vector<SaveObject>();
			Vector<SaveObject> saveLilas = new Vector<SaveObject>();
			Vector<SaveObject> saveAmarelo = new Vector<SaveObject>();
			Vector<SaveObject> saveAzul = new Vector<SaveObject>();
			float timeMiliseq = seq.getMicrosecondLength()/1000;
			float timeArrow;
			
			for (TempObject saveObject : posXVerdeO) {
				timeArrow = ((saveObject.x)*timeMiliseq)/pVerde.getWidth();
				saveVerde.add(new SaveObject(timeArrow,saveObject.key));
			}
			for (TempObject saveObject : posXVermelhoO) {
				timeArrow = ((saveObject.x)*timeMiliseq)/pVerde.getWidth();
				saveVermelho.add(new SaveObject(timeArrow,saveObject.key));
			}
			for (TempObject saveObject : posXLilasO) {
				timeArrow = ((saveObject.x)*timeMiliseq)/pVerde.getWidth();
				saveLilas.add(new SaveObject(timeArrow,saveObject.key));
			}
			for (TempObject saveObject : posXAmareloO) {
				timeArrow = ((saveObject.x)*timeMiliseq)/pVerde.getWidth();
				saveAmarelo.add(new SaveObject(timeArrow,saveObject.key));
			}
			for (TempObject saveObject : posXAzulO) {
				timeArrow = ((saveObject.x)*timeMiliseq)/pVerde.getWidth();
				saveAzul.add(new SaveObject(timeArrow,saveObject.key));
			}
			
			Vector<SaveObject> union = new Vector<SaveObject>();
		    union.addAll(saveVerde);
		    union.addAll(saveVermelho);
		    union.addAll(saveLilas);
		    union.addAll(saveAmarelo);
		    union.addAll(saveAzul);
		    Collections.sort(union);

		   return union;
			
		}

		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}

		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub

		}
	}